/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.xinyue.game.gateway.server.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

/**
 * 游戏网络通信编码Handler，负责处理向网络层发送消息之前，将消息序列化为ByteBuf
 *
 * @author 王广帅
 * @since 2021/5/30 19:12
 */
public class WebSocketEncoderHandler extends ChannelOutboundHandlerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEncoderHandler.class);


    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

//        if (msg instanceof GameMessagePackage) {
//            GameMessagePackage response = (GameMessagePackage) msg;
//            ByteBuf byteBuf = GameMessageCodecService.encode(response, null);
//            byte[] array = byteBuf.array();
//            String text = Base64Utils.encodeToString(array);
//            ctx.channel().writeAndFlush(new TextWebSocketFrame(text));
//        } else {
//            super.write(ctx, msg, promise);
//        }
        ctx.writeAndFlush(msg, promise);
    }
}
